using System;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace StarForce.Hotfix
{
    /// <summary>
    /// 无限滚动
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class LoopCollection<T> where T : ItemContext, new()
    {
        //数组 要具备增删查 功能

        private readonly List<T> m_ItemList = new List<T>();
        
        /// <summary>
        /// 无限滚动脚本
        /// </summary>
        private LoopCollectionBind m_LoopCollectionBind = null;
        
//        private Action<GameObject, int> m_FuncCallBackFunc;
        
//        public event Action<GameObject, int> FuncCallBackFunc
//        {
//            add
//            {
//                m_FuncCallBackFunc -= value; 
//                m_FuncCallBackFunc += value;
//            }
//            remove { m_FuncCallBackFunc -= value; }
//        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="parent"></param>
        public LoopCollection(GameObject parent,Action<GameObject, int> callBack)
        {
            if (!parent.TryGetComponent(out m_LoopCollectionBind))
            {
                Log.Error("ComponentChildBind is valid");
            }
            m_LoopCollectionBind.Init(callBack);
            
        }

        /// <summary>
        /// 展示列表
        /// </summary>
        /// <param name="count">数据长度</param>
        public void ShowList(int count)
        {
            if (m_LoopCollectionBind == null)
            {
                Log.Error("LoopCollectionBind is null");
            }
            m_LoopCollectionBind.ShowList(count);
        }

        /// <summary>
        /// 数据改变时的回调函数
        /// </summary>
        /// <param name="callBack"></param>
        public void SetCallBack(Action<GameObject, int> callBack)
        {
            m_LoopCollectionBind.SetCallback(callBack);
        }
        
        /// <summary>
        /// 插入一个Item
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public T OnItemInsert(GameObject gameObject,int index)
        {
            T item = new T();
            item.Index = index;
            item.GameObject = gameObject;
            item.GetBindComponents();
            m_ItemList.Insert(index, item);
            return item;
        }
        /// <summary>
        /// 获取一个Item
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public T GetItem(GameObject gameObject,int index)
        {
            if (index > m_ItemList.Count || index < 0 || m_ItemList.Count == 0)
            {
                Log.Error("index is outof range");
            }

            T item = m_ItemList[index];
            item.GameObject = gameObject;
            item.GetBindComponents();
            return m_ItemList[index];
        }

        /// <summary>
        /// 获取或添加一个Item
        /// </summary>
        /// <param name="gameObject"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public T GetOrAddItem(GameObject gameObject,int index)
        {
            if (index < 0 || index >= m_ItemList.Count)
            {
                return OnItemInsert(gameObject,index);
            }

            return GetItem(gameObject,index);
        }

        public void Clear()
        {
            for (int i = 0; i < m_ItemList.Count; i++)
            {
                m_ItemList[i].Clear();
                m_ItemList[i].GameObject.SetActive(false);
            }
            m_ItemList.Clear();
            m_LoopCollectionBind.IsClearList = true;
            m_LoopCollectionBind.Num = 0;
        }
    }
}